<?php
namespace V1\Controller;

use V1\Logic\StockLogic;

class QuotationController extends BaseController {
    
     protected $errorCode = array(

        'ranking' => array(
            '-1'=>'用户token失效或有误',
            '-2'=>'参数异常',
        ) ,
        
    );
     /**
     * 概念
     */
    public function concept()
    {
        $data = array("code" => 0,"msg" => "","info"=>array());    
        
        $pageIndex = I('p',1,'intval');
        $number = 20;
        $offset=($pageIndex-1)*$number;
        
        $url ="http://api.gongniucaijing.com/quotation/concept";
        $conf=array(
            'post'=>['number'=>$number,'offset'=>$offset],
        );

        $out = \Org\Net\Http::fsockopenDownload($url,$conf);

        $result = json_decode($out,true);
       
        if($result['status']==0 ){
            $data['info'] = $result['data']['lists'];
        }
        
        $this->responseResult($data);
            
    }

    /**
     * 
     * 行业
     */
    public function industries(){
        
        $data = array("code" => 0,"msg" => "","info"=>array());    
        
        $pageIndex = I('p',1,'intval');
        $number = 20;
        $offset=($pageIndex-1)*$number;
        
        $url ="http://api.gongniucaijing.com/quotation/industries";
        $conf=array(
            'post'=>['number'=>$number,'offset'=>$offset],
        );

        $out = \Org\Net\Http::fsockopenDownload($url,$conf);

        $result = json_decode($out,true);
       
        if($result['status']==0){
            $data['info'] = $result['data']['lists'];
        }
        
        $this->responseResult($data);
            
    }
    public function ranking(){
        $data = array("code" => 0,"msg" => "","info"=>array());    
        
        $id = I('id',0);
        $type = I('type','');
        
        if(empty($id) || empty($type) || !in_array($type, ['industries','concept'])){
             return $this->responseError('-2',200);
        }
        
        $pageIndex = I('p',1,'intval');
        $number = 50;
        $offset=($pageIndex-1)*$number; 
        
        $url ="http://api.gongniucaijing.com/quotation/ranking";
        $conf=array(
            'post'=>['number'=>$number,'offset'=>$offset,'id'=>$id,
                'type'=>$type,'versionCode'=>'190','version'=>'1.9.0',
                'qudao'=>'0000001'],
        );

        $out = \Org\Net\Http::fsockopenDownload($url,$conf);

        $result = json_decode($out,true);
       
        if($result['status']==0 && !empty($result['data']['lists'])){
            
            foreach ($result['data']['lists'] as $key => $value) {
                
                $matiIndicators = StockLogic::getStockMatiIndicators($this->day,$value['code']);
                
                $result['data']['lists'][$key]['stock_order'] =$matiIndicators['stock_order'];
                $result['data']['lists'][$key]['s_index'] =$matiIndicators['s_index'];
                unset( $result['data']['lists'][$key]['recommend']);
                unset( $result['data']['lists'][$key]['stock_status']);
            }
            $data['info'] = $result['data']['lists'];
        }
        
        $this->responseResult($data);
    }
}
